﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using DepSoft.Mobile.WP7.FollwIt.ViewModel;
using Funq;
using GalaSoft.MvvmLight.Messaging;
using WP7Contrib.Common;
using WP7Contrib.Messaging;
using WP7Contrib.Services.Navigation;
using WP7Contrib.Services.Settings;
using WP7Contrib.Services.Storage;

namespace DepSoft.Mobile.WP7.FollwIt
{
    public class BootStrapper : IDisposable
    {
        #region Private members
        private bool _disposed;
        #endregion

        #region Public properties
        public Container Container { get; private set; }
        #endregion

        #region Constructor
        public BootStrapper()
        {
            this.Container = new Container();
            this.ConfigureContainer();
        }
        #endregion

        #region Private methods
        private void ConfigureContainer()
        {
            this.Container.Register<IMessenger>(c => new LastMessageReplayMessenger());

            this.Container.Register<INavigationService>(
                c => new ApplicationFrameNavigationService(((App)Application.Current).RootFrame));

            var assemblies = new List<Assembly> { this.GetType().Assembly, typeof(BaseModel).Assembly };
            this.Container.Register<IStorageService>(c => new StorageService(assemblies));
            this.Container.Register<IStoreSettings>(c => new SettingsStore());

            this.Container.Register(
                c => new MainViewModel(c.Resolve<INavigationService>(), c.Resolve<IStorageService>(), c.Resolve<IStoreSettings>(), c.Resolve<IMessenger>()));

            this.Container.Register(
                c => new CollectionViewModel(c.Resolve<INavigationService>(), c.Resolve<IStorageService>(), c.Resolve<IStoreSettings>(), c.Resolve<IMessenger>()));

            this.Container.Register(
                c => new UserProfileViewModel(c.Resolve<INavigationService>(), c.Resolve<IStorageService>(), c.Resolve<IStoreSettings>(), c.Resolve<IMessenger>()));

            this.Container.Register(
                c => new MovieSummaryViewModel(c.Resolve<INavigationService>(), c.Resolve<IStorageService>(), c.Resolve<IStoreSettings>(), c.Resolve<IMessenger>()));

            this.Container.Register(
                c => new TVShowSummaryViewModel(c.Resolve<INavigationService>(), c.Resolve<IStorageService>(), c.Resolve<IStoreSettings>(), c.Resolve<IMessenger>()));

            this.Container.Register(
                c => new SettingsViewModel(c.Resolve<INavigationService>(), c.Resolve<IStorageService>(), c.Resolve<IStoreSettings>(), c.Resolve<IMessenger>()));

            //this.Container.Register(
            //    c => new AboutViewModel(c.Resolve<INavigationService>(), c.Resolve<IStorageService>(), c.Resolve<IStoreSettings>(), c.Resolve<IMessenger>()));
        }
        #endregion

        #region IDisposable implementation
        public void Dispose()
        {
            if (this.Container != null)
            {
                this.Container.Dispose();
                this.Container = null;
            }
        }
        #endregion
    }
}
